package com.demo.cookie;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/cookie2")
public class CookieDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("request......cookie2..........");
        resp.setContentType("text/html;chatset=utf-8");

        // 获取Cookie数据
        // 可能会获取多个Cookie对象，也可能一个都没有
        Cookie[] cookies = req.getCookies();
        if(cookies != null){
            for(Cookie c : cookies){
                String name = c.getName();
                String value = c.getValue();
                System.out.println(name + ":" + value);
            }
        }


        // 基于本次的请求，给客户端响应数据
        PrintWriter out = resp.getWriter();
        out.write("你好cookie2");
    }
}
